package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrcT;
import cn.e4j.modular.crm.entity.MmrclT;
import cn.e4j.modular.crm.mapper.MmrcTMapper;
import cn.e4j.modular.crm.service.MmrcTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标准售后服务收费项目维护 服务类
 * @author mengyb
 * @date 2019-04-16
 */
@Service
public class MmrcTServiceImpl extends ServiceImpl<MmrcTMapper, MmrcT> implements MmrcTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);
    }
    @Transactional
    @Override
    public void add(MmrcT mmrcT, MmrclT mmrclT) {
        ShiroUser shiroUser= ShiroKit.getUser();

        mmrcT.setMMRCENT(shiroUser.getDBC());
        mmrcT.setMMRCUSER(shiroUser.getACCOUNT());
        mmrcT.setMMRCGRUP(shiroUser.getORG_ID());
        mmrcT.setMMRCORIU(shiroUser.getACCOUNT());
        mmrcT.setMMRCORIG(shiroUser.getORG_ID());
        mmrcT.setMMRCTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrcT.setMMRCDATE(LocalDate.now());

        mmrclT.setMMRCLENT(shiroUser.getDBC());
        mmrclT.setMMRCL001(mmrcT.getMMRC001());
        mmrclT.setMMRCL002(LangUtils.getLang(shiroUser.getLANG()));

        mmrcT.insert();
        mmrclT.insert();

    }
    @Transactional
    @Override
    public void edit(MmrcT mmrcT, MmrclT mmrclT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrcT.setMMRCMODU(shiroUser.getACCOUNT());

        UpdateWrapper<MmrcT> mmrcTUpdateWrapper = new UpdateWrapper<>();

        mmrcTUpdateWrapper.eq("MMRCENT",mmrcT.getMMRCENT());
        mmrcTUpdateWrapper.eq("MMRC001", mmrcT.getMMRC001());

        UpdateWrapper<MmrclT> mmrclTUpdateWrapper=new UpdateWrapper<>();

        mmrclTUpdateWrapper.eq("MMRCLENT",mmrcT.getMMRCENT());
        mmrclTUpdateWrapper.eq("MMRCL001",mmrcT.getMMRC001());

        mmrcT.update(mmrcTUpdateWrapper);
        mmrclT.update(mmrclTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(MmrcT mmrcT, MmrclT mmrclT) {
        UpdateWrapper<MmrcT> mmrcTUpdateWrapper = new UpdateWrapper<>();

        mmrcTUpdateWrapper.eq("MMRCENT",mmrcT.getMMRCENT());
        mmrcTUpdateWrapper.eq("MMRC001", mmrcT.getMMRC001());

        UpdateWrapper<MmrclT> mmrclTUpdateWrapper=new UpdateWrapper<>();

        mmrclTUpdateWrapper.eq("MMRCLENT",mmrcT.getMMRCENT());
        mmrclTUpdateWrapper.eq("MMRCL001",mmrcT.getMMRC001());

        mmrcT.delete(mmrcTUpdateWrapper);
        mmrclT.delete(mmrclTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<MmrcT> mmrcTList) {
        for (MmrcT mmrcT : mmrcTList
        ) {
            UpdateWrapper<MmrcT> mmrcTUpdateWrapper = new UpdateWrapper<>();

            mmrcTUpdateWrapper.eq("MMRCENT",mmrcT.getMMRCENT());
            mmrcTUpdateWrapper.eq("MMRC001", mmrcT.getMMRC001());


            MmrclT mmrclT=new MmrclT();
            UpdateWrapper<MmrclT> mmrclTUpdateWrapper=new UpdateWrapper<>();

            mmrclTUpdateWrapper.eq("MMRCLENT",mmrcT.getMMRCENT());
            mmrclTUpdateWrapper.eq("MMRCL001",mmrcT.getMMRC001());

            mmrcT.delete(mmrcTUpdateWrapper);
            mmrclT.delete(mmrclTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(MmrcT mmrcT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrcT.setMMRCENT(shiroUser.getDBC());
        QueryWrapper<MmrcT> mmrcTQueryWrapper = new QueryWrapper<>();
        mmrcTQueryWrapper.eq("MMRCENT", mmrcT.getMMRCENT());
        mmrcTQueryWrapper.eq("MMRC001", mmrcT.getMMRC001());
        return this.baseMapper.selectCount(mmrcTQueryWrapper)>0?true:false;
    }
}
